package com.fancy.common;

import java.util.Locale;

import org.apache.commons.lang.StringUtils;

/**
 * 登录信息以及多国语言
 * 
 * @author xinfeng.hu 2013-3-13下午12:15:34
 * @version 1.0.0
 * @category 杭州广桥网络技术有限公司(商业先知)
 */
public class LoginContext {

	/**
	 * 用户登录信息
	 */
	private static final ThreadLocal<AuthToken> TOKEN_THREAD_LOCAL = new ThreadLocal<AuthToken>();
	/**
	 * 多国语言
	 */
	private static final ThreadLocal<Locale> I18N_LOCAL = new ThreadLocal<Locale>();

	public static AuthToken get() {
		return TOKEN_THREAD_LOCAL.get();
	}

	public static void set(AuthToken at) {
		TOKEN_THREAD_LOCAL.set(at);
	}

	public static Locale getLocale() {
		return I18N_LOCAL.get();
	}

	public static void setLocale(Locale locale) {
		I18N_LOCAL.set(locale);
	}

	public static void remove() {
		TOKEN_THREAD_LOCAL.remove();
		I18N_LOCAL.remove();
	}

	public static Long getUserId() {
		AuthToken authToken = TOKEN_THREAD_LOCAL.get();
		if (authToken == null) {
			return 0L;
		}
		return TOKEN_THREAD_LOCAL.get().getUserId();
	}

	public static void setUserId(String userId) {
		TOKEN_THREAD_LOCAL.set(new AuthToken(StringUtils.isNumeric(userId) ? Long.valueOf(userId) : 0));
	}
}
